import { Req } from '@nestjs/common';
import { Request } from 'express';
import {
  BeforeInsert,
  BeforeUpdate,
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity('sys_dict_data', { schema: 'lzo_admin_nestjs' })
export class SysDictData {
  @PrimaryGeneratedColumn({ type: 'bigint', name: 'id', comment: '主键' })
  id: string;

  @Column('varchar', {
    name: 'dict_code',
    nullable: true,
    comment: '关联字典编码，与sys_dict表中的dict_code对应',
    length: 50,
  })
  dictCode: string | null;

  @Column('varchar', {
    name: 'value',
    nullable: true,
    comment: '字典项值',
    length: 50,
  })
  value: string | null;

  @Column('varchar', {
    name: 'label',
    nullable: true,
    comment: '字典项标签',
    length: 100,
  })
  label: string | null;

  @Column('varchar', {
    name: 'tag_type',
    nullable: true,
    comment: '标签类型，用于前端样式展示（如success、warning等）',
    length: 50,
  })
  tagType: string | null;

  @Column('tinyint', {
    name: 'status',
    nullable: true,
    comment: '状态（1-正常，0-禁用）',
    default: () => "'0'",
  })
  status: number | null;

  @Column('int', {
    name: 'sort',
    nullable: true,
    comment: '排序',
    default: () => "'0'",
  })
  sort: number | null;

  @Column('varchar', {
    name: 'remark',
    nullable: true,
    comment: '备注',
    length: 255,
  })
  remark: string | null;

  @CreateDateColumn({
    name: 'create_time',
    nullable: true,
    comment: '创建时间',
  })
  createTime: Date | null;

  @Column('bigint', { name: 'createby', nullable: true, comment: '创建人ID' })
  createby: string | null;

  @UpdateDateColumn({
    name: 'update_time',
    nullable: true,
    comment: '更新时间',
  })
  updateTime: Date | null;

  @Column('bigint', { name: 'update_by', nullable: true, comment: '修改人ID' })
  updateBy: string | null;

  // 更新或创建时出入操作人数据,未实现
  @BeforeInsert()
  setCreatedBy(@Req() req: Request) {
    // 从请求上下文中获取当前用户
    this.createby = this.getCurrentUser(req);
  }

  @BeforeUpdate()
  setUpdatedBy(@Req() req: Request) {
    this.updateBy = this.getCurrentUser(req);
  }

  private getCurrentUser(req): string {
    console.log(
      '🚀 ~ dicts-data.entity.ts:107 ~ SysDictData ~ getCurrentUser ~ req:',
      req.user,
    );

    return 'current-user-id'; // 示例值
  }
}
